/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#ifndef __HTTPUSERMANAGER_H__
#define __HTTPUSERMANAGER_H__

#include <rgcpp/structs/Map.h>
#include <pion/net/PionUser.hpp>
#include <rgcpp/tools/Encoder.h>
#include <rgcpp/structs/Types.h>
#include <rgcpp/utils/LogHandler.h>
#include <rgcpp/structs/Singleton.h>
#include <rgcpp/exceptions/RuntimeException.h>

#include "ACSConfig.h"
#include "data/User.h"

namespace acs
{

#define COOKIE_USER_ID "UserID"

class HttpUserThreadBind;
class HttpUserManager : public pion::net::PionUserManager, public rgcpp::Singleton<HttpUserManager>
{
	friend class rgcpp::Singleton<HttpUserManager>;
	friend class HttpUserThreadBind;

	private:
		rgcpp::Map<unsigned int,std::string> userThreadBind;

	public:
		HttpUserManager();
		~HttpUserManager();

		virtual bool addUser(const std::string &username, const std::string &password);
		virtual bool updateUser(const std::string &username, const std::string &password);
		virtual bool addUserHash(const std::string &username, const std::string &password_hash);
		virtual bool updateUserHash(const std::string &username, const std::string &password_hash);
		virtual bool removeUser(const std::string &username);
		virtual pion::net::PionUserPtr getUser(const std::string &username);
		virtual pion::net::PionUserPtr getUser(const std::string& username, const std::string& password);

		User getCurrentUser();

	private:
		void attachUserToThread(std::string userName);
		void detachUserToThread();
};

class HttpUserThreadBind
{
	public:
		HttpUserThreadBind(pion::net::PionUserPtr userPtr)
		{
			if(userPtr.get())
				HttpUserManager::getInstance()->attachUserToThread(userPtr->getUsername());
		}

		~HttpUserThreadBind()
		{
			HttpUserManager::getInstance()->detachUserToThread();
		}
};

}

#endif

